#include "vsf.h"
#include "core.h"

#define W600_PLL_CLK_HZ			(160 * 1000 * 1000)

static struct vsfhal_info_t vsfhal_info =
{
	0, CORE_VECTOR_TABLE, OSC0_FREQ_HZ, CPU_FREQ_HZ
};

vsf_err_t vsfhal_core_get_info(struct vsfhal_info_t **info)
{
	*info = &vsfhal_info;
	return VSFERR_NONE;
}

vsf_err_t vsfhal_core_reset(void *p)
{
	NVIC_SystemReset();
	return VSFERR_NONE;
}

vsf_err_t vsfhal_core_init(void *p)
{
	uint32_t reg = tls_reg_read32(HR_CLK_DIV_CTL);
	uint8_t wlanDiv = (reg >> 4) & 0x0F;
	uint8_t cpuDiv = W600_PLL_CLK_HZ / vsfhal_info.cpu_freq_hz;

	reg &= 0xFFFFF000;
	reg |= 0x80000000;
	reg |= ((wlanDiv * 4 / cpuDiv) << 8) | (wlanDiv << 4) | cpuDiv;
	tls_reg_write32(HR_CLK_DIV_CTL, reg);
	vsfhal_info.apb_freq_hz = W600_PLL_CLK_HZ / 4 / ((reg >> 4) & 0x0F);
	vsfhal_info.wlan_freq_hz = W600_PLL_CLK_HZ / ((reg >> 4) & 0x0F);

	tls_reg_write32(HR_GPIOA_AFSEL, 0x0);
	tls_reg_write32(HR_GPIOB_AFSEL, 0x0);

//	SCB->VTOR = vsfhal_info.vector_table;
	NVIC_SetPriorityGrouping(vsfhal_info.priority_group);
	return VSFERR_NONE;
}

#include "hal/arch/arm_cm/arm_cm.h"
